package com.bs.areport.service.master;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import oracle.net.aso.l;

import com.bs.areport.service.util.DataUtil;
import com.bs.areport.service.util.EntityType;
import com.bs.areport.service.util.MethodUtil;
import com.bs.restframework.context.util.SystemContextUtil;
import com.bs.restframework.db.Database;
import com.bs.restframework.session.UserSession;

/**
 * 
 * <p>函数名称：权限服务类</p>
 * <p>创建时间：2012-03-08</p>
 * <p>作者：李新宇</p>
 */
public class UserRightDao {
	
	/**
	 * 
	 * <p>函数名称：userTree</p>
	 * <p>功能说明：获取用户树/p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-13</p>
	 * <p>作者：李新宇</p>
	 */
	public List<Map> userTree(Map<String, Object> objects) throws Exception
	{	
		
		Database db = SystemContextUtil.getDatabase(this);
		String sql="";
		String id = (String)objects.get("id");
		
		String deptSql="";
		String deptGpConditon="";
		String deptUConditon="";
		String deptJc=MethodUtil.getDeptJC(db);

		
		//获取用户单位信息
		String token =(String)objects.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession(token);
		String deptCode=(String)userSession.getAttribute("deptCode");
		String loginUserID=userSession.getUserID();
		
		if (deptCode!=null&&!"".equals(deptCode)){
			Integer deptlevel=DataUtil.getLevel(deptJc, deptCode);
			deptSql=" LEFT JOIN "+EntityType.ARP_BSC_DEPT_TABLE+" D ON D.F_ID=M.F_DEPTID";
			deptGpConditon=" AND D.F_CODE LIKE '"+deptCode+"%' AND (D.F_LEVEL="+deptlevel+")";
			deptUConditon=" AND D.F_CODE LIKE '"+deptCode+"%' AND (D.F_LEVEL="+deptlevel+" OR (D.F_LEVEL="+(deptlevel+1)+" AND F_USERTYPE<>'P' ) )";
		}

		
		if (id == "" || id == null){
			sql ="SELECT Z.* FROM (";
			sql+="( SELECT M.F_ID AS \"id\", '[' || M.F_CODE || ']' || M.F_NAME AS \"text\" ,";
		    sql+= " 'closed' AS \"state\",M.F_CODE,'G' as TYPE FROM "+EntityType.SYS_USERGROUP_TABLE+" M";
		    sql+=deptSql;
		    sql+=" WHERE 1=1 ";
		    sql+=deptGpConditon;
		    sql+=" ) UNION ALL ( ";
			sql+=" SELECT M.F_ID AS \"id\", '[' || M.F_CODE || ']' || M.F_NAME AS \"text\" ,";
		    sql+= " '' AS \"state\",M.F_CODE,'U' as TYPE  FROM "+EntityType.SYS_USER_TABLE+" M ";
		    sql+=deptSql;
		    sql+=" WHERE F_USERGROUPID IS NULL AND F_USERTYPE<>'A' AND M.F_ID<>'"+loginUserID+"'";
		    sql+=deptUConditon;
		    sql+=" )) Z ORDER BY Z.TYPE,Z.F_CODE ";
		}
		else
		{
			sql=" SELECT M.F_ID AS \"id\", '[' || M.F_CODE || ']' || M.F_NAME AS \"text\" ,";
		    sql+= " '' AS \"state\" FROM "+EntityType.SYS_USER_TABLE+" M ";
		    sql+=deptSql;
		    sql+=" WHERE F_USERGROUPID='"+id+"' AND F_USERTYPE<>'A' AND M.F_ID<>'"+loginUserID+"'";
		    sql+=deptUConditon;
		    sql+=" ORDER BY M.F_CODE ";
		}
		
		List<Map> listMap = db.queryList(Map.class, sql,null); 
		List<Map> resultList= null;
		if(id==null||"".equals(id)){
			Map<String, Object> root=new HashMap<String, Object>();
			root.put("id", "");
			root.put("text", "所有");
			root.put("children", listMap);
			resultList=new ArrayList<Map>();
			resultList.add(root);
			
		}else {
			resultList=listMap;
		}		
		
		return resultList;
	}
	
	/**
	 * 
	 * <p>函数名称：rightTree</p>
	 * <p>功能说明：获取/p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public List<Map> rightTree(Map<String, Object> valueMap) throws Exception
	{	
		Database db = SystemContextUtil.getDatabase(this);
		String token =(String)valueMap.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession(token);
		String userID=userSession.getUserID();
		
		String sql="";
		String id = (String)valueMap.get("id");
		
		List<Map> resultList=null;
		
		sql =" SELECT M.F_ID AS \"id\", '[' || M.F_CODE || ']' || M.F_NAME AS \"text\" ,";
		sql += " CASE M.F_ISLEAF WHEN 0 THEN 'closed' ELSE '' END AS \"state\" FROM "+EntityType.SYS_RIGHT_TABLE+" M ";
		if (!"".equals(id)&& id != null){
		    sql += " LEFT JOIN "+EntityType.SYS_USERRIGHT_TABLE+" U ON U.F_RIGHTID=M.F_ID ";
		}
		sql += " WHERE 1=1 ";
		if ("".equals(id) || id == null){
			    sql += " AND M.F_LEVEL<=1 " ;
		}
		else
		{
			String sqlcode="SELECT F_CODE,F_LEVEL FROM "+EntityType.SYS_RIGHT_TABLE+" WHERE F_ID='"+id+"'";
			Map fthrNode=db.queryObject(Map.class, sqlcode, null);
			Integer level=(Integer.parseInt(fthrNode.get("F_LEVEL").toString())+1);
			
			sql+=" AND U.F_USERID='"+userID+"'";
			sql += " AND M.F_LEVEL="+level.toString()+" AND M.F_CODE LIKE '" + fthrNode.get("F_CODE") + "%' " ;
		}
		sql+=" ORDER BY to_number(M.F_CODE)" ;
		List<Map> listMap = db.queryList(Map.class, sql,null); 	

		resultList=listMap;
		
		return resultList;
	}
	
	/**
	 * 
	 * <p>函数名称：getUserRight</p>
	 * <p>功能说明：获取用户权限/p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-13</p>
	 * <p>作者：李新宇</p>
	 */	
	public List<Map> getUserRight(Map<String, Object> valueMap)  throws Exception
	{
		Database db = SystemContextUtil.getDatabase(this);
		
		List<Map> list=DataUtil.getList(db, valueMap, EntityType.SYS_USERRIGHT,true, false, false);
		
		return list;
	}
	
	
	/**
	 * 
	 * <p>函数名称：updateUserRight</p>
	 * <p>功能说明：更新用户权限/p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-13</p>
	 * <p>作者：李新宇</p>
	 */	
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateUserRight(String userId,String rightIds)  throws Exception
    {
    	Database db = SystemContextUtil.getDatabase(this);
		//最后修改时间
        Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);		
    	Map<String, Object> valueMap=new HashMap<String, Object>();
    	valueMap.put("F_USERID", userId);
    	
    	
    	DataUtil.del(db, valueMap, EntityType.SYS_USERRIGHT, false, false);
    	
    	valueMap.put("F_LASTTIME", dateString);
    	
    	String[] rights=rightIds.split("\\|");
    	
    	for (String rightID : rights) {
    		if(rightID!=null&&!"".equals(rightID)){
	    		valueMap.put("F_RIGHTID", rightID);
	    		DataUtil.add(db, valueMap, EntityType.SYS_USERRIGHT);	
    		}
		}
    }

}
 